package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.adapter.FeatureDiaryAdapter;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.MyArticle;
import com.selfdriving.bean.MyArticleDetails;
import com.selfdriving.bean.ScenicNotes;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

public class TravelNotes extends BaseFragmentActivity
{

	MoMoRefreshListView listView;
	FeatureDiaryAdapter adapter;
	private List<Entity> notesList = new ArrayList<Entity>();
	int page = 1;
	private TextView title;
	private ImageButton imgBreak;
	String sid;
	String name;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.refresh_listview_have_word);
		initViews();
		initEvents();
		getListDatas(false);
	}

	@Override
	protected void initViews()
	{
		Intent intent = super.getIntent();
		listView = (MoMoRefreshListView) findViewById(R.id.refresh_list);
		adapter = new FeatureDiaryAdapter(mApplication, this, notesList, 0);
		title = (TextView) super.findViewById(R.id.top_text_middle);
		imgBreak = (ImageButton) super.findViewById(R.id.head_break);
		imgBreak.setVisibility(View.VISIBLE);
		sid = intent.getStringExtra("sid");
		name = intent.getStringExtra("title");
		title.setText(name + "游记");
		listView.setAdapter(adapter);
	}

	@Override
	protected void initEvents()
	{
		listView.setOnRefreshListener(new OnRefreshListener()
		{
			@Override
			public void onRefresh()
			{
				// TODO Auto-generated method stub
				page = 1;
				getListDatas(false);
			}

			@Override
			public void onLoadMore()
			{
				// TODO Auto-generated method stub
				getListDatas(true);
			}
		});
		listView.setOnCancelListener(new OnCancelListener()
		{

			@Override
			public void onCancel()
			{
				// TODO Auto-generated method stub
				listView.onRefreshComplete();
			}
		});
		listView.setOnItemClickListener(onItemClickListener);
		imgBreak.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
	}

	OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3)
		{
			if (arg2 > 0)
			{
				MyArticle myArticle;
				myArticle = (MyArticle) notesList.get(arg2 - 1);
				post(myArticle.getId());
			}
		}
	};

	public void post(String id)
	{
		Utils.showLoadingDialog(this, "数据加载中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "19");
		params.put("id", id);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONObject object = new JSONObject(json);
					String id = object.getString("id");
					String content = object.getString("content");
					String title = object.getString("title");
					String uid = object.getString("uid");
					String uname = object.getString("uname");
					String times = object.getString("times");
					String laud = object.getString("laud");
					String marrow = object.getString("marrow");
					String clicks = object.getString("clicks");
					String img0 = null;
					String img1 = null;
					String img2 = null;
					if (object.has("img0"))
					{
						img0 = object.getString("img0");
					}
					if (object.has("img1"))
					{
						img1 = object.getString("img1");
					}
					if (object.has("img2"))
					{
						img2 = object.getString("img2");
					}
					mApplication.myArticleDetails = new MyArticleDetails(id,
							content, title, uid, uname, times, laud, marrow,
							clicks, img0, img1, img2);
					Utils.dismissLoadingDialog();
					Intent intent = new Intent(TravelNotes.this,
							AboutMeMyArticleDetails.class);
					startActivity(intent);
				} catch (JSONException e)
				{
					Utils.dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}

	public void getListDatas(final boolean loadMost)
	{
		if (!loadMost)
		{
			notesList.clear();
		}
		listView.mState = 2;
		listView.changeHeaderViewByState();
		AjaxParams params = new AjaxParams();
		params.put("f", "18");
		params.put("type", "0");
		params.put("sid", sid);
		params.put("page", "" + page);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(20000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				listView.onRefreshComplete();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				System.out.println(json);
				if (!json.contains("empty"))
				{
					try
					{
						MyArticle myArticle;
						JSONArray jsonArray = new JSONArray(json);
						for (int i = 0; i < jsonArray.length(); i++)
						{
							JSONObject object = jsonArray.getJSONObject(i);
							String id = object.getString("id");
							String title = object.getString("title");
							String headImg = object.getString("headImg");
							String uname = object.getString("uname");
							String times = object.getString("times");
							String laud = object.getString("laud");
							String marrow = object.getString("marrow");
							String clicks = object.getString("clicks");
							String review = object.getString("review");
							String img = object.getString("img");
							myArticle = new MyArticle(id, title, headImg,
									uname, times, laud, marrow, clicks, review,
									img);
							notesList.add(myArticle);
						}
						adapter.notifyDataSetChanged();
						listView.onRefreshComplete();
						isLoadMost(loadMost);
						if (notesList.size() > 5)
						{
							listView.addFooterView(listView.mFootView);
						}
						page++;
					} catch (JSONException e)
					{
						listView.onRefreshComplete();
					}
				} else
				{
					showShortToast("当前无" + name + "游记信息");
					listView.onRefreshComplete();
				}
			}
		});
	}

	public void isLoadMost(boolean is)
	{
		if (is)
		{
			listView.onLoadMoreComplete(false);
		}
	}

	@Override
	public void onBackPressed()
	{
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
}
